/**
 * @file 订单标签配置
 * @author liuying
 * @date 2021-07-30 15:24:11
 */

layui.define(['element', 'layer', 'form', 'table', 'zmnOmsSelect', 'zmnSelect', 'laytpl', 'laydate', '$http'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let $http = layui.$http;
    let layer = layui.layer;
    let laydate = layui.laydate;
    let zmnOmsSelect = layui.zmnOmsSelect;
    let zmnSelect = layui.zmnSelect;
    var table = layui.table;
    var defaultToolbars = ["filter"];

    let CONF_BASE_URL = OMS_BASE_URL + '/price/feedback';

    let params = {};

    /**
     * 渲染表格
     * @param {object} params 查询参数
     * @returns {object}
     */
    function renderTable() {
        table.render({
            elem: '#price-feedback-table',
            url: CONF_BASE_URL + '/ajax/list.action',
            toolbar: '#security-tips',
            where: getSearchParams(),
            defaultToolbar: defaultToolbars,
            page: true,
            cols: [[
                {field: 'feedbackId', align: 'center', title: 'ID', width: 50},
                {field: 'engineerId', align: 'center', title: '工程师Id', width: 100},
                {field: 'cityName', align: 'center', title: '所在城市', width: 100},
                {field: 'channelName', align: 'center', title: '渠道名称', width: 100},
                {field: 'erpServCateg', align: 'center', title: '后台服务分类', width: 100},
                {field: 'erpCategOne', align: 'center', title: '后台一级分类', width: 100},
                {field: 'erpCategTwo', align: 'center', title: '后台二级分类', width: 100},
                {field: 'erpProductName', align: 'center', title: '后台产品名称', width: 100},
                {field: 'erpProductId', align: 'center', title: '后台产品ID', width: 100},
                {field: 'projectName', align: 'center', title: '项目名称', width: 100},
                {field: 'projectId', align: 'center', title: '项目ID', width: 100},
                {field: 'productType', align: 'center', title: '产品类型', width: 100},
                {field: 'brand', align: 'center', title: '品牌', width: 100},
                {field: 'tariffId', align: 'center', title: '价格表ID', width: 100},
                {field: 'tariffName', align: 'center', title: '价格表名称', width: 100},
                {field: 'tariffCalMode', align: 'center', title: '定价方式', width: 100},
                {field: 'feedbackPriceStr', align: 'center', title: '反馈执行价', width: 100},
                {field: 'expectPriceStr', align: 'center', title: '期望价格', width: 100},
                {field: 'remark', align: 'center', title: '备注说明', width: 100},
                {field: 'createTime', align: 'center', title: '反馈时间', width: 100},
            ]],
            request: {
                pageName: 'pageIndex', // 页码
                limitName: 'pageSize' // 条数限制
            },
            response: {
                statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                var count = res.data ? res.data.count : 0;
                $('.layui-this').find('.count').text('（共 ' + count + ' 条）');
                return {
                    "code": res.status,
                    "msg": res.message,
                    "count": res.data ? (res.data.count > 10000 ? 10000 : res.data.count) : 0, //解析数据长度
                    "data": res.data ? res.data.data : [] //解析数据列表
                };
            },
            done: function (res) {
                $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
            }
        });
    }

    if ($("#export_button").val() === "true") {
        defaultToolbars.push( 'exports');
    }

    // 监听重置按钮点击
    form.on('submit(submit-reset)', function (obj) {
        $('select').val('');
        $('input').val('');
        // 字段全部置空
        Object.keys(obj.field).filter((key) => {
            obj.field[key] = '';
        });
        table.reload("price-feedback-table", {
            where: getSearchParams(obj.field),
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        return false;
    });

    // 查询
    form.on('submit(submit-search)', function (obj) {
        table.reload("price-feedback-table", {
            where: getSearchParams(obj.field),
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        return false;
    });

    table.on("toolbar(price-feedback-table)", function (obj) {
        switch (obj.event) {
            case 'CUSTOM_EXPORT':
                let param = $("#item_form_list").serializeArray();
                $.ajax({
                    async: false,
                    type: "GET", url: "/price/feedback/exportData",
                    dataType: "json", data: param,
                    traditional:true,
                    success: function (json) {
                        console.log(json)
                    }
                })
                top.layui.index.openTabsPage(json.zmn_url_env + 'elephant.xiujiadian.com/tapi/export/list.action', "导出文件管理");
                return;
        }
    });

    // 服务产品分类组件
    zmnSelect.selectlErpProdCategories({
        servCategSelectId: 'erpServCategId',
        erpCategOneSelectId: 'erpCategOneId',
        erpCategTwoSelectId: 'erpCategTwoId',
    }, {
        servCategSelectId: {
            isRadio: true,
            selectedValues: "",
        },
        erpCategOneSelectId: {
            isRadio: true,
            selectedValues: "",
        },
        erpCategTwoSelectId: {
            isRadio: true,
            selectedValues: "",
        }
    });

    // 城市组件
    zmnSelect.selectCity('cityId', {
        isRadio: true,
        selectedValues: "",
        page:{
            enable: true,
            pageSize: 10
        },
    });

    // 渠道组件
    zmnSelect.searchChannel('channelId', {
        isRadio: true,
    });

    //日期选择器
    laydate.render({
        elem: '#feedbackDateRange',
        range: "/",
    });

    function getSearchParams(field) {
        if (field) {
            // 拷贝非空字段
            Object.keys(field).filter((key) => {
                params[key] = field[key];
            });
        }
        return params;
    }


    renderTable();
    exports('oms_price_feedback_list');
});
